package com.unience.ec2.eclipse.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.SWT;

import com.unience.ec2.eclipse.config.Config;
import com.xerox.amazonws.ec2.AddressInfo;
import com.xerox.amazonws.ec2.AvailabilityZone;
import com.xerox.amazonws.ec2.EC2Exception;
import com.xerox.amazonws.ec2.Jec2;

public class ListAvailabilityZonesViewPart extends ViewPart {
    public static final String ID_VIEW =
        "com.unience.ec2.eclipse.view.ListAvailabilityZonesViewPart"; //$NON-NLS-1$

    Composite composite1;
    
    private Table tableZones;
    
    
    /**
     * 
     */
    public ListAvailabilityZonesViewPart() {
        super();
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    public void createPartControl(Composite parent) {
    	parent.setSize(956, 600);
    	{
    		composite1 = new Composite(parent, SWT.NULL);
    		GridLayout composite1Layout = new GridLayout();
    		composite1Layout.makeColumnsEqualWidth = true;
    		composite1Layout.horizontalSpacing = 1;
    		composite1.setLayout(composite1Layout);
    		
    		{
    			GridData tableElasticIPLData = new GridData();
    			tableElasticIPLData.widthHint = 300;
    			tableElasticIPLData.heightHint = 264;
    			tableZones = new Table(composite1, SWT.NONE);
    			tableZones.setLayoutData(tableElasticIPLData);
    			TableColumn tc1 = new TableColumn(tableZones, SWT.CENTER);
    			TableColumn tc2 = new TableColumn(tableZones, SWT.CENTER);
                
                tc1.setText("Zone");
                tc2.setText("State");
                
                
                tc1.setWidth(150);
                tc2.setWidth(150);
            
                
                tableZones.setHeaderVisible(true);
                
                
    		}
    	}
    	loadZones();
	}
	
	
    private void loadZones(){
    	
    	Config config = Config.getInstance();
    	Jec2 jec2 =  config.getEc2Instance().getEc2();
    	
    	try {
			List<AvailabilityZone> zones = jec2.describeAvailabilityZones(new ArrayList<String>());
			
			for (AvailabilityZone zone : zones) {
				
				TableItem item = new TableItem(tableZones, SWT.NONE);
				item.setText(new String[] {zone.getName(), zone.getState()});
				
				
			}
			
		} catch (EC2Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
     */
    public void setFocus() {
        // TODO Auto-generated method stub
    }
    
    /**
     * Cleans up all resources created by this ViewPart.
     */
    public void dispose() {
        super.dispose();
    }
    
}
